package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseGoodsInfo;
import com.xteam.ecsystem.service.admin.GoodsService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.system.ConfigManager;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.GoodsQueryCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

@Controller
public class GoodsContorller {
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private SystemLogService logService;
	
	@Autowired
	private ConfigManager cfgManager;

	public void setToBeJsonType(HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};

	@RequestMapping("/getGoodsList.do")
	public void getGoodsList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String goodsQueryStr = ReqHelper.getRequestString(request,
				"goodsQueryData");
		GoodsQueryCenterStation goodsStation = null;
		if (StringUtil.isNotNullOrEmpty(goodsQueryStr)) {
			JSONObject obj = JSONObject.fromObject(goodsQueryStr);
			goodsStation = (GoodsQueryCenterStation) JSONObject.toBean(obj,
					GoodsQueryCenterStation.class);
		}
		String strList = this.goodsService.getGoodsList(pageIndex, pageSize,
				goodsStation);
		response.getWriter().write(strList);
	}

	@RequestMapping("/saveOrUpdateGoodsInfo.do")
	public void saveOrUpdateObject(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "goodsInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		
		// 将JSON中的日期注册能够识别Date类型的BeanMorpher
		MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
		morpherRegistry.registerMorpher(new DateMorpher(
				new String[] { "yyyy-MM-dd HH:mm" }));
		BaseGoodsInfo goodsInfo = (BaseGoodsInfo) JSONObject.toBean(obj,
				BaseGoodsInfo.class);
		
		// goodsInfo.setBgiOn(StringUtil.strToDate(obj.getString("bgiOn"),
		// "yyyy-MM-dd HH:mm"));
		// goodsInfo.setBgiOff(StringUtil.strToDate(obj.getString("bgiOff"),
		// "yyyy-MM-dd HH:mm"));
		
		if(StringUtil.isNotNullOrEmpty(goodsInfo.getBgiId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODS_MGR, 
				"saveOrUpdateObject", lslType, "BaseGoodsInfo");
		
		String bgiId = this.goodsService.saveOrUpdateObject(goodsInfo,
				goodsInfo.getBgiId());
		
		response.getWriter().write(bgiId);
	}

	@RequestMapping("/getGoodsInfoById.do")
	public void getGoodsInfoById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String strInfo = this.goodsService.getGoodsInfoById(bgiId);
		response.getWriter().write(strInfo);
	}

	@RequestMapping("/updateGoodsStateById.do")
	public void updateGoodsStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String isRevert = ReqHelper.getRequestString(request, "isRevert");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		this.goodsService.updateGoodsStateById(bgiId);
		
		if("isRevert".equals(isRevert)){
			this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSTRASH_MGR, 
					"updateGoodsStateById", ConstantLocation.LOG_REVERT, "BaseGoodsInfo");
		}else{
			this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODS_MGR, 
					"updateGoodsStateById", ConstantLocation.LOG_GOODS_TRA, "BaseGoodsInfo");
		}
	}

	@RequestMapping("/updateBgiBestById.do")
	public void updateBgiBestById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		this.goodsService.updateBgiBestById(bgiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODS_MGR, 
				"updateBgiBestById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsInfo");
	}

	@RequestMapping("/updateBgiNewById.do")
	public void updateBgiNewById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		this.goodsService.updateBgiNewById(bgiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODS_MGR, 
				"updateBgiNewById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsInfo");
	}

	@RequestMapping("/updateBgiHotById.do")
	public void updateBgiHotById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		this.goodsService.updateBgiHotById(bgiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODS_MGR, 
				"updateBgiHotById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsInfo");
	}

	@RequestMapping("/updateBgiBuyById.do")
	public void updateBgiBuyById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		this.goodsService.updateBgiBuyById(bgiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODS_MGR, 
				"updateBgiBuyById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsInfo");
	}

	@RequestMapping("/getBggTreeSelect.do")
	public void getBggTreeSelect(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String type = ReqHelper.getRequestString(request, "type");
		String bggTreeSelect = this.goodsService.getBggTreeSelect(type);
		response.getWriter().write(bggTreeSelect);
	}

	@RequestMapping("/getBgbCombobox.do")
	public void getBgbCombobox(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String bgbCombobox = this.goodsService.getBgbCombobox();
		response.getWriter().write(bgbCombobox);
	}

	@RequestMapping("/getBgsCombobox.do")
	public void getBgsCombobox(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String bgsCombobox = this.goodsService.getBgsCombobox();
		response.getWriter().write(bgsCombobox);
	}

	@RequestMapping("/getAllGoodsTypeAttr.do")
	public void getAllGoodsTypeAttr(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String btaGtype = ReqHelper.getRequestString(request, "btaGtype");
		String strInfo = this.goodsService.getAllGoodsTypeAttr(btaGtype);
		response.getWriter().write(strInfo);
	}

	@RequestMapping("/saveGoodsAttr.do")
	public void saveGoodsAttr(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String btaGtype = ReqHelper.getRequestString(request, "btaGtype");
		String dynamicJsonStr = ReqHelper.getRequestString(request, "dynamicJson");
		String staticJsonStr = ReqHelper.getRequestString(request, "staticJson");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
	
		JSONArray dynamicArray = JSONArray.fromObject(dynamicJsonStr);
		JSONArray staticArray = JSONArray.fromObject(staticJsonStr);
		
		this.goodsService.saveGoodsAttr(bgiId, btaGtype, dynamicArray,
				staticArray);
		
		if ((dynamicArray != null && dynamicArray.size() > 0) 
				|| (staticArray != null && staticArray.size() > 0)) {
			this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSATTR_MGR, 
					"saveGoodsAttr", ConstantLocation.LOG_SAVE, "BaseGoodsAttribute");
		}
	}

	@RequestMapping("/getGoodsAttrByBgiId.do")
	public void getGoodsAttrByBgiId(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String strInfo = this.goodsService.getGoodsAttrByBgiId(bgiId);
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/getGoodsAttrTab.do")
	public void getGoodsAttrTab(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String bgaGoods = ReqHelper.getRequestString(request, "bgiId");
		String strInfo = this.goodsService.getGoodsAttrTab(bgaGoods);
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/getBgtByBggId.do")
	public void getBgtByBggId(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String bgaGgroup = ReqHelper.getRequestString(request, "bggId");
		String strInfo = this.goodsService.getBgtByBggId(bgaGgroup);
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/getBgaTabData.do")
	public void getBgaTabData(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bggId = ReqHelper.getRequestString(request, "bggId");
		String strInfo = this.goodsService.getBgaTabData(bggId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/getBgaTabPrint.do")
	public void getBgaTabPrint(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String bgiGroup = ReqHelper.getRequestString(request, "bgiGroup");
		String strInfo = this.goodsService.getBgaTabPrint(bgiId, bgiGroup);
		
		response.getWriter().write(strInfo);
	}

	@RequestMapping("/getGoodsPicture.do")
	public void getGoodsPicture(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String strInfo = goodsService.getGoodsPicture(pageIndex, pageSize,
				bgiId);
		response.getWriter().write(strInfo);
	}

//	@RequestMapping("/saveGoodsPicture.do")
//	public void saveGoodsPicture(HttpServletRequest request,
//			HttpServletResponse response, ModelMap modelMap) throws IOException {
//		String goodsPictureDate = ReqHelper.getRequestString(request,
//				"goodsPicture");
//		BaseGoodsPicture goodsPicture = null;
//		if (goodsPictureDate != null && goodsPictureDate != ""
//				&& !"null".equals(goodsPictureDate)) {
//			JSONObject obj = JSONObject.fromObject(goodsPictureDate);
//			goodsPicture = (BaseGoodsPicture) JSONObject.toBean(obj,
//					BaseGoodsPicture.class);
//		}
//		goodsPicture.setBgpTime(new Date());
//		goodsService.saveOrUpdatePicture(goodsPicture, goodsPicture.getBgpId(), this.cfgManager.getServerConfig());
//	}
	
	@RequestMapping("/updatePics.do")
	public void updatePics(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String[] bgpId = ReqHelper.getRequestStrings(request, "bgpId");
		String[] bgpDesc = ReqHelper.getRequestStrings(request, "bgpDesc");
		String[] bgpState = ReqHelper.getRequestStrings(request, "bgpState");
		String[] bgpOrder = ReqHelper.getRequestStrings(request, "bgpOrder");
		String[] isDeleted =ReqHelper.getRequestStrings(request, "isDeleted");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		goodsService.updatePics(bgpId,bgpDesc,bgpState,bgpOrder,isDeleted);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODS_MGR_PIC, 
				"updatePics", ConstantLocation.LOG_MODIFY, "BaseGoodsPicture");
	}
	
	@RequestMapping("/uploadGoodsPic.do")
	public void uploadGoodsPic(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String fileNames = ReqHelper.getRequestString(request, "fileNames");
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		
		goodsService.uploadGoodsPic(fileNames, bgiId, this.cfgManager.getServerConfig());
	}

//	@RequestMapping("/delPicture.do")
//	public void delPicture(HttpServletRequest request,
//			HttpServletResponse response, ModelMap modelMap) throws IOException {
//		String bgpId = ReqHelper.getRequestString(request, "bgpId");
//		goodsService.delPicture(bgpId);
//	}

//	@RequestMapping("/findPicture.do")
//	public void findPicture(HttpServletRequest request,
//			HttpServletResponse response, ModelMap modelMap) throws IOException {
//		String bgpId = ReqHelper.getRequestString(request, "bgpId");
//		String str = goodsService.findPicture(bgpId);
//		response.getWriter().write(str);
//	}

	@RequestMapping("/getGoodsTrashList.do")
	public void getGoodsTrashList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bgiKeyword = ReqHelper.getRequestString(request, "bgiKeyword");
		String strList = this.goodsService.getGoodsTrashList(pageIndex,
				pageSize, bgiKeyword);
		response.getWriter().write(strList);
	}

	@RequestMapping("/delGoodsByIds.do")
	public void delGoodsByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgiIds = ReqHelper.getRequestString(request, "bgiIds");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		this.goodsService.delGoods(bgiIds);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSTRASH_MGR, 
				"delGoods", ConstantLocation.LOG_DELETE_RES, "1-BaseGoodsAttribute，2-BaseGoodsInfo");
	}

	@RequestMapping("/revertGoodsByIds.do")
	public void revertGoodsByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgiIds = ReqHelper.getRequestString(request, "bgiIds");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		this.goodsService.saveRevertGoods(bgiIds);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSTRASH_MGR, 
				"saveRevertGoods", ConstantLocation.LOG_BATCH_REVERT, "BaseGoodsInfo");
	}

	@RequestMapping("/addAccessories.do")
	public void addAccessories(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgiIds = ReqHelper.getRequestString(request, "bgiIds");
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		goodsService.saveAccessories(bgiIds, bgiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODS_MGR_ACC, 
				"saveAccessories", ConstantLocation.LOG_MODIFY, "BaseGoodsAccessories");
	}

	@RequestMapping("/getAccessoriesList.do")
	public void getAccessoriesList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String strList = this.goodsService.getAccessoriesList(pageIndex,
				pageSize, bgiId);
		response.getWriter().write(strList);
	}

	@RequestMapping("/addAssociation.do")
	public void addAssociation(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String brgRgoods = ReqHelper.getRequestString(request, "brgRgoods");
		String brgIsDoulbes = ReqHelper.getRequestString(request,"brgIsDoulbes");
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		
		goodsService.saveAssociation(brgRgoods, brgIsDoulbes, bgiId);
	}
	
	@RequestMapping("/saveBrgInfo.do")
	public void saveBrgInfo(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String brgRgoods = ReqHelper.getRequestString(request, "brgRgoods");
		String brgIsDoulbes = ReqHelper.getRequestString(request,"brgIsDoulbes");
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		goodsService.saveBrgInfo(brgRgoods, brgIsDoulbes, bgiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODS_MGR_ASS, 
				"saveBrgInfo", ConstantLocation.LOG_MODIFY, "BaseRefGoods");
	}

	@RequestMapping("/getAssociationList.do")
	public void getAssociationList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String strList = this.goodsService.getAssociationList(pageIndex,
				pageSize, bgiId);
		response.getWriter().write(strList);
	}

	@RequestMapping("/addGoodsAricles.do")
	public void addGoodsAricles(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		String baiIds = ReqHelper.getRequestString(request, "baiIds");
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		this.goodsService.saveGoodsAricles(baiIds, bgiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODS_MGR_ART, 
				"saveGoodsAricles", ConstantLocation.LOG_MODIFY, "BaseRefArticle");
	}

	@RequestMapping("/getArticlesList.do")
	public void getArticlesList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bgiId = ReqHelper.getRequestString(request, "bgiId");
		String strList = this.goodsService.getArticlesList(pageIndex, pageSize,
				bgiId);
		response.getWriter().write(strList);
	}
}
